﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.AnalysisServices.AdomdClient;

namespace RetailAnalytic.Tools.SaveLocalCube
{
    public static class LocalCubeService
    {
        public static void Recurse2(object oPM, object f, List<string> memb, AdomdConnection conn, string cubeName)
        {
            object c = RuntimeHelpers.GetObjectValue(LateBinding.LateGet(f, null, "Constants", new object[0], null, null));
            object[] _Vb_t_array_1 = new object[] { RuntimeHelpers.GetObjectValue(oPM) };
            bool[] _Vb_t_array_2 = new bool[] { true };
            if (_Vb_t_array_2[0])
            {
                oPM = RuntimeHelpers.GetObjectValue(_Vb_t_array_1[0]);
            }
            Microsoft.Office.Interop.Owc11.PivotFilterUpdateMemberStateEnum nState = ((Microsoft.Office.Interop.Owc11.PivotMember)oPM).Field.FieldSet.CreateFilterUpdate.get_StateOf((Microsoft.Office.Interop.Owc11.PivotMember)oPM);
            if (nState == Microsoft.Office.Interop.Owc11.PivotFilterUpdateMemberStateEnum.plMemberStateChecked)
            {
                string[] ll = { "", "", "" };
                memb.Add((string)(((Microsoft.Office.Interop.Owc11.PivotMember)oPM).UniqueName));
                //Microsoft.Office.Interop.Owc11.PivotMemberFindFormatEnum ee = Microsoft.Office.Interop.Owc11.PivotMemberFindFormatEnum.plFindFormatPathInt;
            }
            if (nState == Microsoft.Office.Interop.Owc11.PivotFilterUpdateMemberStateEnum.plMemberStateGray)
            {
                memb.Add((string)(((Microsoft.Office.Interop.Owc11.PivotMember)oPM).UniqueName));
                short _Vb_t_i2_0 = ShortType.FromObject(ObjectType.SubObj(LateBinding.LateGet(LateBinding.LateGet(oPM, null, "ChildMembers", new object[0], null, null), null, "Count", new object[0], null, null), 1));
                for (short i = 0; i <= _Vb_t_i2_0; i = (short)(i + 1))
                {
                    object[] _Vb_t_array_0 = new object[] { i };
                    _Vb_t_array_2 = new bool[] { true };
                    if (_Vb_t_array_2[0])
                    {
                        i = ShortType.FromObject(_Vb_t_array_0[0]);
                    }
                    if (conn.Cubes[cubeName.Substring(cubeName.IndexOf("[") + 1, cubeName.IndexOf("]") - 1)].Dimensions[memb[0]].Hierarchies[memb[1]].HierarchyOrigin != HierarchyOrigin.UserHierarchy)
                    {
                        Recurse2(RuntimeHelpers.GetObjectValue(LateBinding.LateGet(oPM, null, "ChildMembers", _Vb_t_array_0, null, _Vb_t_array_2)), f, memb, conn, cubeName);
                    }
                }
            }
        }

        public static bool GetIntersectionForMembers(string name, List<List<string>> chDim)
        {
            bool inter = false;
            for (int i = 0; i <= chDim.Count - 1; i++)
            {
                List<string> listDim = chDim[i];
                if (listDim[3] == "P")
                { inter = false; }
                else
                {
                    if (listDim[0] + "." + listDim[1] == name)
                    {
                        inter = true;
                        break;
                    }
                }
            }
            return inter;
        }

        public static List<string> GetDimensionsFromFilter(string filter)
        {
            List<string> filterList = new List<string>();
            while (filter.IndexOf("Измерение: <") != -1)
            {
                string dim = String.Empty;
                dim = filter.Substring(filter.IndexOf("Измерение: <") + 12, filter.IndexOf(">") - 12);
                filter = filter.Substring(filter.IndexOf(">") + 1, filter.Length - filter.IndexOf(">") - 1);
                string dim1 = filter.Substring(filter.IndexOf("Иерархия: <") + 11, filter.IndexOf(">") - 12);
                if (dim1.IndexOf(".") != -1)
                {
                    dim1 = dim1.Substring(dim1.IndexOf(".") + 1, dim1.Length - dim1.IndexOf(".") - 1);
                }
                dim += "." + dim1;
                if ((filter.IndexOf("Измерение: <") != -1))
                    filter = filter.Substring(filter.IndexOf("Измерение: <"), filter.Length - filter.IndexOf("Измерение: <"));
                filterList.Add(dim);
            }
            return filterList;
        }
    }
}
